package vistas;

import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenu;
import javax.swing.table.DefaultTableModel;

import clases.Departamentos_class;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import javax.swing.JMenuItem;
import javax.swing.ImageIcon;
import java.awt.Cursor;
import javax.swing.JScrollPane;
import java.awt.Toolkit;

public class Buscardepartamentosform extends JInternalFrame {
	private JTextField textField;
	private JTable tabla_departamentos;
	private int filas;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Buscardepartamentosform dialog = new Buscardepartamentosform();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the dialog.
	 */
	public Buscardepartamentosform() {
		setFrameIcon(new ImageIcon(Asignarhorarios_buscarform.class.getResource("/icons/extra.png")));
		setClosable(true);
		setIconifiable(true);
		setTitle("Buscar Departamento");
		setBounds(100, 100, 543, 387);
		getContentPane().setLayout(null);
		filas=0;
		JLabel label = new JLabel("Nombres");
		label.setFont(new Font("Tahoma", Font.PLAIN, 13));
		label.setBounds(10, 13, 59, 16);
		getContentPane().add(label);
		
		textField = new JTextField();
		textField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField.setColumns(10);
		textField.setBounds(79, 11, 296, 20);
		getContentPane().add(textField);
		
		JButton button = new JButton("Buscar");
		button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		button.setContentAreaFilled(false);
		button.setIcon(new ImageIcon(Buscardepartamentosform.class.getResource("/icons/buscar.png")));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodoBuscar();
			}
		});
		button.setBounds(385, 11, 120, 23);
		getContentPane().add(button);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 40, 507, 298);
		getContentPane().add(scrollPane);
		
		tabla_departamentos = new JTable();
		scrollPane.setViewportView(tabla_departamentos);
		tabla_departamentos.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"codigodepartamentos", "Nombre"
			}
		));
		tabla_departamentos.getColumnModel().getColumn(0).setPreferredWidth(15);
		tabla_departamentos.getColumnModel().getColumn(0).setMinWidth(0);
		tabla_departamentos.getColumnModel().getColumn(0).setMaxWidth(0);
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(tabla_departamentos, popupMenu);
		
		JMenuItem mntmModificar = new JMenuItem("Modificar");
		mntmModificar.setIcon(new ImageIcon(Buscardepartamentosform.class.getResource("/icons/modificar_pq.png")));
		mntmModificar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
					if(tabla_departamentos.getSelectedRow()>-1)
					{
						Departamentosingresarform departamentos = new Departamentosingresarform(tabla_departamentos.getValueAt(tabla_departamentos.getSelectedRow(), 0).toString());
						Principal.contenedor.add(departamentos);
						departamentos.setVisible(true);
					}
			}
		});
		popupMenu.add(mntmModificar);
		metodoBuscar();
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	private void metodoBuscar()
	{

		try
		{
			filas=0;
			ResultSet resultado = new Departamentos_class().mostrar();
			DefaultTableModel mdepartamentos = (DefaultTableModel)tabla_departamentos.getModel();
			while(mdepartamentos.getRowCount()>0) mdepartamentos.removeRow(0);
			while(resultado.next())
			{
				mdepartamentos.addRow(new Object[filas]);
				mdepartamentos.setValueAt(resultado.getString("codigodepartamento"), filas,0);
				mdepartamentos.setValueAt(resultado.getString("nombre"), filas,1);
				filas++;
			}
			resultado.close();
		}catch(Exception ex)
		{
			
		}
	
	}
}
